﻿# based on claim_cb

event_claim_cb = {
	icon = claim_cb
	group = event

	mutually_exclusive_titles = {
		NOT = {
			# If pushing personal claims, you can press multiple claims with the Divine Right innovation (High Medieval Era).
			trigger_if = {
				limit = {
					scope:attacker = scope:claimant
				}
				scope:attacker = {
					OR = {
						culture = {
							has_innovation = innovation_chronicle_writing
						}
						AND = {
							government_has_flag = government_is_landless_adventurer
							has_realm_law = camp_purpose_legitimists
							has_variable = legitimist_claimed_title
							var:legitimist_claimed_title = {
								OR = {
									holder = scope:defender
									de_jure_liege = scope:defender
								}
							}
						}
					}
				}
			}
			# If pushing claims for someone else, you need the Rightful Ownership innovation to press multiple claims (Late Medieval Era).
			trigger_else = {
				scope:attacker = {
					culture = {
						has_innovation = innovation_divine_right
					}
				}
			}
		}
	}

	# This whole section was already commented out, I just commented it out more to prevent console errors
	#allowed_for_character = {}

	# allowed_against_character = {
	# 	faith.religion = scope:attacker.faith.religion
	# }

	# Root is the title
	# scope:claimant is the claimant
	# scope:attacker is the attacker
	# scope:defender is the defender
	is_allowed_claim_title = {
		trigger_if = {
			limit = {
				scope:attacker = {
					is_ai = yes
					ai_has_conqueror_personality = yes
				}
			}
			tier >= tier_duchy
		}
		custom_description = {
			text = "claimant_titles_held_by_you_or_vassal"
			NOR = {
				holder = scope:attacker
				holder = {
					target_is_liege_or_above = scope:attacker
				}
			}
		}
		scope:claimant = {
			NOT = { has_trait = incapable }
			is_hostage = no
			trigger_if = {
				limit = { NOT = { scope:claimant = scope:attacker } } # You can always press your own claims
				is_imprisoned = no
			}
			#religion = scope:attacker.religion
			# claimant must not be at war against defender for this claim already
			custom_description = {
				text = "claimant_already_pressing_their_claim"
				object = root
				NAND = {
					liege = scope:attacker
					any_character_war = {
						casus_belli = {
							any_target_title = {
								this = root
							}
						}
					}
				}
			}
		}
		scope:attacker = {
			#Cannot press claims against your own vassals, as these must be revoked.
			custom_description = {
				text = "claimant_titles_held_by_attacker_vassals"
				NOT = {
					any_vassal_or_below = {
						has_title = root
					}
				}
			}
		}
		trigger_if = {
			limit = {
				NOT = { scope:claimant = scope:attacker } # You can always press your own claims (though the prestige cost might be high)
			}
			trigger_if = {
				limit = {
					is_weak_claimant_due_to_gender_trigger = { FAITH = scope:attacker.faith CHARACTER = scope:claimant }
				}
				trigger_if = {
					limit = {
						scope:claimant = { is_adult = yes }
					}
					save_temporary_scope_as = treat_claim_as_weak
				}
				trigger_else = {
					always = no
				}
			}
			trigger_else_if = {
				limit = {
					scope:claimant = { is_adult = no }
				}
				save_temporary_scope_as = treat_claim_as_weak
			}
			# making the default behavior explicit if no other trigger_(else_)ifs are valid
			trigger_else = {
				always = yes
			}
		}
		trigger_if = {
			limit = { exists = scope:treat_claim_as_weak }
			holder = {
				custom_description = {
					text = "claim_target_position_is_weak"
					object = scope:attacker
					OR = {
						has_trait = incapable
						is_adult = no
						is_weak_claimant_due_to_gender_trigger = { FAITH = scope:attacker.faith CHARACTER = root.holder }
					}
				}
			}
		}
		#Under Byzantine law, a ruler must be unblemished
		trigger_if = {
			limit = {
				scope:defender.culture = { has_cultural_parameter = physical_disfigurement_blocks_inheritance }
			}
			scope:claimant = {
				custom_description = {
					text = "byzantine_claimant_is_disfigured"
					object = scope:claimant
					is_eunuch_trigger = no
					NOR = {
						has_trait = incapable
						has_trait = blind
						has_trait = disfigured
						has_trait = maimed
						has_trait = one_eyed
						has_trait = one_legged
						has_trait = clubfooted
						has_trait = hunchbacked
						has_trait = dwarf
					}
				}
			}
		}
		#Under Byzantine law, a ruler must be able to produce children.
		trigger_if = {
			limit = {
				scope:defender.culture = { has_cultural_parameter = physical_disfigurement_blocks_inheritance }
			}
			scope:claimant = {
				custom_description = {
					text = "byzantine_claimant_is_celibate"
					object = scope:defender
					is_eunuch_trigger = no
					NOR = {
						has_trait = celibate
					}
				}
			}
		}
	}

	allowed_for_character_display_regardless = {
		#If you're landless, you need the Legitimist camp purpose
		trigger_if = {
			limit = {
				scope:attacker = {
					government_has_flag = government_is_landless_adventurer
				}
			}
			scope:attacker = {
				has_realm_law = camp_purpose_legitimists
			}
		}
	}

	cost = {}

	target_titles = claim
	#target_title_tier = all

	on_declaration = {
		on_declared_war = yes
		if = {
			limit = {
				scope:defender = { is_sibling_of = scope:attacker }
			}
			scope:attacker = {
				add_achievement_flag_effect = { FLAG = achievement_bad_blood_flag }
			}
		}
	}

	should_invalidate = {
		OR = {
			NOT = {
				any_in_list = {
					list = target_titles
					any_in_de_jure_hierarchy = {
						tier >= tier_county
						exists = holder
						holder = {
							OR = {
								this = scope:defender
								target_is_liege_or_above = scope:defender
							}
						}
					}
				}
			}
			AND = { 
				NOT = { scope:claimant = scope:attacker } 
				scope:claimant = {
					OR = {
						is_alive = no 
						NOR = { 
							is_vassal_or_below_of = scope:attacker 
							is_courtier_of = scope:attacker 
						}
					}
				}
			} 
		}
	}

	on_invalidated_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					NOT = { scope:claimant = scope:attacker } 
					scope:claimant = { is_alive = no }
				}
				desc = msg_claim_war_invalidated_claimant_unavailable_dead_message
			}
			triggered_desc = {
				trigger = {
					scope:claimant = {
						NOR = { 
							this = scope:attacker 
							NOR = { 
								is_vassal_or_below_of = scope:attacker 
								is_courtier_of = scope:attacker 
							}
						}
					}
				}
				desc = msg_claim_war_invalidated_claimant_unavailable_not_vassal_message
			}
			desc = msg_claim_war_invalidated_message
		}
	}
	
	on_invalidated = {
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:claimant = { is_local_player = yes }
				}
				desc = claim_cb_victory_desc_attacker_claimant
			}
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = claim_cb_victory_desc_attacker
			}
			desc = claim_cb_victory_desc
		}
	}

	on_victory = {
		scope:attacker = {
			if = {
				limit = {
					is_ai = no
					has_realm_law = camp_purpose_legitimists
					any_in_list = {
						list = target_titles
						tier >= tier_kingdom
					}
				}
				add_achievement_global_variable_effect = {
					VARIABLE = finished_birthright_achievement
					VALUE = yes
				}
			}
			if = {
				limit = {
					is_landless_adventurer = yes
					NOT = { government_has_flag = government_is_nomadic }
				}
				hidden_effect = {
					if = {
						limit = {
							NOT = { has_trait = adventurer }
						}
						add_trait = adventurer
					}
					save_scope_as = the_adventurer
					every_courtier = {
						set_variable = {
							name = former_camp_leader
							value = scope:the_adventurer
						}
						add_trait = adventurer_follower
					}
				}
			}
			if = {
				limit = {
					has_variable = install_claimants_oath
					has_variable = install_claimants_oath_counter
				}
				add_to_variable_list = {
					name = installed_claimants_oath
					target = scope:claimant
				}
				change_variable = {
					name = install_claimants_oath_counter
					add = 1
				}
			}
		}
	
		scope:attacker = { show_pow_release_message_effect = yes }

		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		# Influence
		add_influence_attacker_victory_effect = yes

		#EP2 Accolade glory gain from winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_med_effect = yes }

		scope:defender ?= {
			if = {
				limit = {
					is_independent_ruler = yes
					government_allows = administrative
					primary_title = { is_in_list = target_titles }
				}
				every_held_title = {
					limit = {
						is_noble_family_title = no
						tier >= tier_county
					}
					add_to_list = target_titles
				}
			}
		}

		create_title_and_vassal_change = {
			type = conquest_claim
			save_scope_as = change
			add_claim_on_loss = yes
		}

		# EP3 Laamp possibility warning
		scope:defender = { ep3_war_loss_adventurer_tt_effect = yes }
		
		# Create a claim CB which is executed to handle title/vassal changes, in addition to being used to calculate Prestige awards for war participants.
		setup_claim_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			change = scope:change
		}

		resolve_title_and_vassal_change = scope:change

		if = {
			limit = {
				NOT = { scope:claimant = scope:attacker }
				scope:claimant.primary_title.tier < scope:attacker.primary_title.tier
				scope:claimant = {
					NOT = { target_is_liege_or_above = scope:attacker }
				}
			}

			create_title_and_vassal_change = {
				type = conquest_claim
				save_scope_as = change_two
				add_claim_on_loss = yes
			}
			scope:claimant = {
				change_liege = {
					liege = scope:attacker
					change = scope:change_two
				}
			}
			resolve_title_and_vassal_change = scope:change_two
		}

		if = {
			limit = {
				NOT = { scope:claimant = scope:attacker }
				scope:attacker = {
					can_add_hook = {
					  	type = favor_hook
					  	target = scope:claimant
					}
				}
			}
			scope:attacker = {
				add_hook = {
		  			target = scope:claimant
		  			type = favor_hook
		 		}
			}
		}

		# Attacker gets Prestige Experience, Defender loses Prestige, all other participants gain Prestige based on their war contribution.
		modify_all_participants_fame_values = {
			WINNER = scope:attacker
			LOSER = scope:defender
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = 10
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}

		# truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = claim_cb_white_peace_desc_defender
			}
			desc = claim_cb_white_peace_desc
		}
		
	}

	on_white_peace = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			stress_impact = {
 				ambitious = medium_stress_impact_gain
 				arrogant = medium_stress_impact_gain
 			}
		}

		#EP2 accolade glory gain for defender doing alright
		scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

		# press claims
		every_in_list = {
			list = target_titles
			if = {
				limit = { scope:claimant = { has_weak_claim_on = prev } }
				scope:claimant = {
					make_claim_strong = prev
				}
			}
		}

		# Create an 'helper' claim CB which we won't execute, but can use to determine how much Prestige should be given to war participants.
		setup_claim_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			victory = no
		}
		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = scope:cb_prestige_factor # Set by 'setup_claim_cb'
				multiply = -5.0
			}
		}
		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}
		# Defender neither gains nor loses any prestige.
		scope:defender = {
			stress_impact = {
 				arrogant = medium_stress_impact_gain
 			}
		}

		if = {
			limit = {
				NOT = { scope:claimant = scope:attacker }
				scope:attacker = {
					can_add_hook = {
					  	type = favor_hook
					  	target = scope:claimant
					}
				}
			}
			scope:attacker = {
				add_hook = {
		  			target = scope:claimant
		  			type = favor_hook
		 		}
			}
		}

		# Truce
		add_truce_white_peace_effect = yes
		
		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = claim_cb_defeat_desc_defender
			}
			triggered_desc = {
				trigger = { scope:claimant = { is_local_player = yes } }
				desc = claim_cb_defeat_desc_attacker_claimant
			}
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = claim_cb_defeat_desc_attacker
			}
			desc = claim_cb_defeat_desc
		}
		
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		# Influence
		add_influence_attacker_defeat_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

		# lose claims
		every_in_list = {
			list = target_titles
			scope:claimant = {
				remove_claim = prev
			}
		}

		if = {
			limit = {
				NOT = { scope:claimant = scope:attacker }
				scope:attacker = {
					can_add_hook = {
					  	type = favor_hook
					  	target = scope:claimant
					}
				}
			}
			scope:attacker = {
				add_hook = {
		  			target = scope:claimant
		  			type = favor_hook
		 		}
			}
		}

		# Attacker pays gold to the defender as reparations.
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 3
			}
		}
		# Create an 'helper' claim CB which we won't execute, but can use to determine how much Prestige should be given to war participants.
		setup_claim_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			victory = no
		}
		# Attacker loses Prestige, all other war participants gain Prestige (Defender gains full prestige, all allies on both sides gain based on war contribution).
		modify_all_participants_fame_values = {
			WINNER = scope:defender
			LOSER = scope:attacker
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = scale_10_war_defender_win
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}

		# Truce
		add_truce_attacker_defeat_effect = yes

		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "CLAIM_WAR_NAME"
	my_war_name = "CLAIM_WAR_NAME_MY"
	war_name_base = "CLAIM_WAR_NAME_BASE"
	cb_name = "CLAIM_CB_NAME"
	interface_priority = 60

	should_show_war_goal_subview = yes

	attacker_wargoal_percentage = 0.8
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 1000

	ai_score_mult = {
		value = 1
		#There is no fighting in the admin room!
		if = {
			limit = {
				scope:attacker = {
					government_has_flag = government_is_feudal
					liege ?= {
						government_has_flag = government_is_administrative
					}
				}
				scope:defender = {
					government_has_flag = government_is_administrative
					liege ?= scope:attacker.liege
				}
			}
			multiply = 0.4
		}
		#Declaring war on the frankokratia leader can really ruin the whole thing
		if = {
			limit = {
				scope:defender = {
					any_owned_story = {
						story_type = frankokratia_story
						NOT = { has_variable = franko_war_complete }
					}
				}
			}
			multiply = 0
		}
	}
}
